#include "StdAfx.h"
#include "UIMenuOption.h"

CMenuOptionUI::CMenuOptionUI(void)
{
	m_bCombie = false;
	m_bShowTip = FALSE;
	m_bLButtonPressed = false;
	m_bDragStart = false;
}

CMenuOptionUI::~CMenuOptionUI(void)
{
}

void CMenuOptionUI::SetAttribute(LPCTSTR pstrName, LPCTSTR pstrValue)
{
	if( _tcscmp(pstrName, _T("selectarrowimage")) == 0 ) SetSelectedArrowImage(pstrValue);
	else if( _tcscmp(pstrName, _T("selectforeimage")) == 0 ) SetSelectedForeImage(pstrValue);
	else if(_tcscmp(pstrName, _T("tipleftimage")) == 0 ) SetTipLeftImage(pstrValue);
	else if(_tcscmp(pstrName, _T("tipcenterimage")) == 0 ) SetTipCenterImage(pstrValue);
	else if(_tcscmp(pstrName, _T("tiprightimage")) == 0 ) SetTipRightImage(pstrValue);
	else if(_tcscmp(pstrName, _T("tiptext")) == 0 ) SetTipText(pstrValue);
	else if(_tcscmp(pstrName, _T("tipTextfont")) == 0 ) SetTipTextFont(_ttoi(pstrValue));
	else if(_tcscmp(pstrName, _T("tipTextColor")) == 0 ) SetTipTextFont(_ttoi(pstrValue));
	else if(_tcscmp(pstrName, _T("tipimage")) == 0 ) SetTipImage(pstrValue);
	COptionUI::SetAttribute(pstrName, pstrValue);
}

void CMenuOptionUI::SetSelectedForeImage( LPCTSTR pstrValue )
{
	m_sSelectedForeImage = pstrValue;
	Invalidate();
}

void CMenuOptionUI::SetSelectedArrowImage( LPCTSTR pstrValue )
{
	m_sSelectedArrowImage = pstrValue;
	Invalidate();
}

void CMenuOptionUI::PaintStatusImage( HDC hDC )
{
	if( (m_uButtonState & UISTATE_SELECTED) != 0 ) 
	{
		if( !m_sSelectedArrowImage.IsEmpty() ) 
		{
			if( !DrawImage(hDC, (LPCTSTR)m_sSelectedArrowImage) ) 
			{
				m_sSelectedImage.Empty();
			}
		}
	}

	COptionUI::PaintStatusImage(hDC);

	if( (m_uButtonState & UISTATE_SELECTED) != 0 ) 
	{
		if( !m_sSelectedForeImage.IsEmpty() ) 
		{
			if( !DrawImage(hDC, (LPCTSTR)m_sSelectedForeImage) ) 
			{
				m_sSelectedForeImage.Empty();
			}
		}
	}

	if(!m_sTipText.IsEmpty())
	{
		PaintTip(hDC);
	}
	if(!m_sTipImage.IsEmpty() && m_bShowTip)
	{
		PaintTip2(hDC);
	}
}

LPCTSTR CMenuOptionUI::GetClass() const
{
	return _T("MenuOptionUI");
}

bool CMenuOptionUI::ShouldDrowForeImage()
{
	return (bool)( (m_uButtonState & UISTATE_SELECTED) == 0 );
}

void CMenuOptionUI::SetTipLeftImage( LPCTSTR pstrValue )
{
	m_sTipLeftImage = pstrValue;
	Invalidate();
}

void CMenuOptionUI::SetTipCenterImage( LPCTSTR pstrValue )
{
	m_sTipCenterImage = pstrValue;
	Invalidate();
}

void CMenuOptionUI::SetTipRightImage( LPCTSTR pstrValue )
{
	m_sTipRightImage = pstrValue;
	Invalidate();
}

void CMenuOptionUI::SetTipText( LPCTSTR pstrValue )
{
	m_sTipText = pstrValue;
	Invalidate();
}

void CMenuOptionUI::SetTipTextFont( int iFont )
{
	m_iTipTextFont = iFont;
	Invalidate();
}

void CMenuOptionUI::SetTipTextCorlor( DWORD dwValue )
{
	m_dwTipTextColor = dwValue;
}


void CMenuOptionUI::PaintTip2( HDC hDC )
{
	if(!DrawImage(hDC, (LPCTSTR)m_sTipImage))
	{
		m_sTipImage.Empty();
	}
}

void CMenuOptionUI::PaintTip( HDC hDC )
{
	if(!DrawImage(hDC, (LPCTSTR)m_sTipLeftImage))
	{
		m_sTipLeftImage.Empty();
	}

	RECT rcTipText = {0, 0, 0, 0};
	HFONT hTipFont = m_pManager->GetFont(m_iTipTextFont);
	HFONT hFont= (HFONT)::SelectObject(hDC, hTipFont);

	::SetBkMode(hDC, TRANSPARENT);
	::SetTextColor(hDC, RGB(255, 255, 255));
	::DrawText(hDC, m_sTipText, m_sTipText.GetLength(), &rcTipText, DT_LEFT|DT_SINGLELINE | DT_VCENTER |DT_NOPREFIX | DT_CALCRECT);
	int nTipTextWidth = rcTipText.right - rcTipText.left;

	CDuiString strCenterImagerDest, strRightImageDest;
	strCenterImagerDest.Format(_T(" dest='%d,%d,%d,%d'"), 
		TIPTEXTLEFTPOS, TIPTOP, TIPTEXTLEFTPOS + nTipTextWidth, TIPBOTTOM);
	strRightImageDest.Format(_T(" dest='%d,%d,%d,%d'"), 
		TIPTEXTLEFTPOS + nTipTextWidth, TIPTOP, TIPTEXTLEFTPOS + nTipTextWidth + TIPIMAGEWIDTH, TIPBOTTOM);


	if(!m_bCombie)
	{
		m_sTipCenterImage += strCenterImagerDest;
		m_sTipRightImage += strRightImageDest;
		m_bCombie = true;
	}
	

	if(!DrawImage(hDC, (LPCTSTR)m_sTipCenterImage))
	{
		m_sTipCenterImage.Empty();
	}
	
	
	if(!DrawImage(hDC, (LPCTSTR)m_sTipRightImage))
	{
		m_sTipRightImage.Empty();
	}

	rcTipText.bottom = TIPBOTTOM - TIPTOP;
	::OffsetRect(&rcTipText, TIPTEXTLEFTPOS, TIPTOP);
	::OffsetRect(&rcTipText, m_rcItem.left, m_rcItem.top);

	::DrawText(hDC, m_sTipText, m_sTipText.GetLength(), &rcTipText, DT_LEFT|DT_SINGLELINE | DT_VCENTER |DT_NOPREFIX);
	hTipFont = (HFONT)::SelectObject(hDC, hFont);
}

void CMenuOptionUI::PaintText( HDC hDC )
{
	if( (m_uButtonState & UISTATE_SELECTED) != 0 || m_sTipText.IsEmpty())
	{
		return;
	}
	else
	{
		COptionUI::PaintText(hDC);
	}
}

void CMenuOptionUI::SetTipImage( LPCTSTR pstrValue )
{
	m_sTipImage = pstrValue;
}

void CMenuOptionUI::ShowTip2( BOOL bShowTip )
{
	m_bShowTip = bShowTip;
	Invalidate();
}

void CMenuOptionUI::DoEvent( TEventUI& event )
{
	if( event.Type == UIEVENT_BUTTONDOWN )
	{
		__super::DoEvent(event);
		m_bLButtonPressed = true;
		m_bDragStart = false;
		m_ptMosueDown = event.ptMouse;
		OutputDebugString(_T("UIEVENT_BUTTONDOWN\n"));
		return;
	}

	if( event.Type == UIEVENT_BUTTONUP )
	{
		__super::DoEvent(event);
		m_bLButtonPressed = false;
		m_bDragStart = false;
		OutputDebugString(_T("UIEVENT_BUTTONUP\n"));
		return;
	}

	if( event.Type == UIEVENT_MOUSEMOVE )
	{
		if(!m_bDragStart && m_bLButtonPressed && (abs(m_ptMosueDown.x - event.ptMouse.x) >= 4 || abs(m_ptMosueDown.y - event.ptMouse.y) >= 4))
		{
			m_pManager->SendNotify(this, DUI_MSGTYPE_MAINMENU_DRAGSTART);
			m_bDragStart = true;
		}

		return;
	}

	__super::DoEvent(event);
}


